<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        ref="crud"
        :page.sync="page"
        :data="tableData"
        :table-loading="tableLoading"
        :permission="permissionList"
        :option="tableOption"
        @on-load="getList"
        @search-change="searchChange"
        @refresh-change="refreshChange"
        @size-change="sizeChange"
        @current-change="currentChange"
        @row-update="handleUpdate"
        @row-save="handleSave"
        @row-del="rowDel"
      >
        <template slot="menuLeft">
          <el-button
            class="filter-item"
            size="small"
            @click="downloadTask()"
            type="warning"
            icon="el-icon-download"
          >导出</el-button>
          <el-button
            class="filter-item"
            size="small"
            @click="showLogInfo"
            type="info"
            icon="el-icon-tickets"
          >日志</el-button>
        </template>
        <template slot="isPause" slot-scope="scope">
          <el-tag type="danger" v-if="scope.row.isPause === true">暂停</el-tag>
          <el-tag type="success" v-if="scope.row.isPause === false">启动</el-tag>
        </template>
        <template slot-scope="scope" slot="menu">
          <el-button icon="el-icon-document" type="text" @click="executionTask(scope.row)">执行</el-button>
          <el-button
            icon="el-icon-document"
            type="text"
            v-if="scope.row.isPause == true"
            @click="updateQStatus(scope.row)"
          >启用</el-button>
          <el-button
            icon="el-icon-document"
            type="text"
            v-if="scope.row.isPause == false"
            @click="updateZStatus(scope.row)"
          >暂停</el-button>
        </template>
      </avue-crud>
    </basic-container>
    <task-deipatch-log ref="taskDeipatchLog"></task-deipatch-log>
  </div>
</template>

<script>
import {
  fetchList,
  downloadObj,
  getObj,
  addObj,
  putObj,
  executionObj,
  delObj
} from "@/admin/api/sys-quartz-job";
import { tableOption } from "@/admin/const/sys-quartz-job";
import { download } from "~/admin/api/common";
import { mapGetters } from "vuex";
import taskDeipatchLog from "~/admin/views/system-manage/sys-conf/task-deipatch-log";
import { downloadBlob } from "~/utils/index";

export default {
  name: "sysquartzjob",
  components: { taskDeipatchLog },
  data() {
    return {
      searchForm: {},
      tableData: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      tableLoading: false,
      tableOption: tableOption
    };
  },
  computed: {
    ...mapGetters(["permissions"]),
    permissionList() {
        return {
            addBtn: this.vaildData(this.permissions.sys_quartz_job_add, false),
            delBtn: this.vaildData(this.permissions.sys_quartz_job_del, false),
            editBtn: this.vaildData(this.permissions.sys_quartz_job_edit, false),
            updateStatusBtn: this.vaildData(this.permissions.sys_quartz_job_update_status, false),
            executionBtn: this.vaildData(this.permissions.sys_quartz_job_execution, false)
        };
    }
  },
  methods: {
    getList(page, params) {
      this.tableLoading = true;
      fetchList(
        Object.assign(
          {
            current: page.currentPage,
            size: page.pageSize
          },
          params,
          this.searchForm
        )
      )
        .then(response => {
          this.tableData = response.data.data.records;
          this.page.total = response.data.data.total;
          this.tableLoading = false;
        })
        .catch(() => {
          this.tableLoading = false;
        });
    },
    rowDel: function(row, index) {
      this.$confirm("是否确认删除ID为" + row.id, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          let ids =[];
          ids.push(row.id);
          return delObj(ids);
        })
        .then(data => {
          this.$message.success("删除成功");
          this.getList(this.page);
        });
    },

    handleUpdate: function(row, index, done, loading) {
      this.tableLoading = true;
      putObj(row)
        .then(data => {
          this.$message.success("修改成功");
          this.getList(this.page);
          this.tableLoading = false;
          done();
        })
        .catch(() => {
          this.tableLoading = false;
        });
    },
    executionTask: function(row) {
      this.tableLoading = true;
      executionObj(row.id)
        .then(data => {
          this.$message.success("执行成功");
          this.getList(this.page);
          this.tableLoading = false;
        })
        .catch(() => {
          this.tableLoading = false;
        });
    },
    downloadTask: function() {
      var time = new Date();
      time.getFullYear(); //获取完整的年份(4位,1970-???)
      time.getMonth(); //获取当前月份(0-11,0代表1月)
      time.getDate(); //获取当前日(1-31)
      time.getHours(); //获取当前小时数(0-23)
      time.getMinutes(); //获取当前分钟数(0-59)
      time.getSeconds(); //获取当前秒数(0-59)

      let name =
        time.getFullYear() +
        "-" +
        (time.getMonth() + 1) +
        "-" +
        time.getDate() +
        " " +
        time.getHours() +
        "_" +
        time.getMinutes() +
        "_" +
        time.getSeconds() +
        "_" +
        "定时任务数据.xlsx";
      let url = "/admin/task/jobs/download";
      download(url, this.searchForm).then(response => {
        downloadBlob(response.data, name);
      });
    },
    updateQStatus: function(row) {
      this.tableLoading = true;
      getObj(row.id)
        .then(data => {
          this.$message.success("修改状态成功");
          this.getList(this.page);
          this.tableLoading = false;
        })
        .catch(() => {
          this.tableLoading = false;
        });
    },
    updateZStatus: function(row) {
      this.tableLoading = true;
      getObj(row.id)
        .then(data => {
          this.$message.success("修改状态成功");
          this.getList(this.page);
          this.tableLoading = false;
        })
        .catch(() => {
          this.tableLoading = false;
        });
    },
    handleSave: function(row, done, loading) {
      let params = Object.assign({}, row);
      delete params.createTime;
      addObj(params)
        .then(data => {
          this.$message.success("添加成功");
          this.getList(this.page);
          done();
        })
        .catch(() => {});
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    currentChange(current) {
      this.page.currentPage = current;
    },
    searchChange(form, done) {
      this.searchForm = form;
      this.page.currentPage = 1;
      this.getList(this.page, form);
      done();
    },
    refreshChange() {
      this.getList(this.page);
    },
    showLogInfo() {
      this.$refs.taskDeipatchLog.show();
    }
  }
};
</script>
